myApp.controller('catalogController',function($scope,toaster,$uibModal,$serCatalog,$state,FileUploader,$serImgUpload){
    var ctrl = this;
    ctrl.vm = {
        data:{},
        daP:{},
        addData:{},
        panDuan:'',
        bianJi:'',
        addCatalog:0,
        test1:function(item){
            ctrl.vm.addData = {};
            ctrl.vm.daP = item;
            ctrl.vm.panDuan = 1;
            ctrl.vm.bianJi =0;
            ctrl.vm.addCatalog=0;
        },
        test2:function(list){
            ctrl.vm.addData = {};
            ctrl.vm.daP = list;
            ctrl.vm.panDuan = 2;
            ctrl.vm.bianJi =0;
            ctrl.vm.addCatalog=0;
        },
        testtt:function(test){
            ctrl.vm.addData = {};
            ctrl.vm.daP = test;
            ctrl.vm.panDuan = 0;
            ctrl.vm.addCatalog=0;
            ctrl.vm.bianJi =0;
        },
        saveChange:function(){
            $serCatalog.updateIt({
                id:ctrl.vm.daP.id,
                name:ctrl.vm.daP.name,
                status:ctrl.vm.daP.status,
                parent:ctrl.vm.daP.parent,
                sort:ctrl.vm.daP.sort,
                level:ctrl.vm.daP.level,
                is_hot:ctrl.vm.daP.is_hot,
                image:ctrl.vm.daP.image,
                icon:ctrl.vm.daP.icon
            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.addData = {};
                    toaster.success('提示',res.message);
                    vm.init();
                    ctrl.vm.daP.id=0 //清空信息栏
                    ctrl.vm.bianJi!==0
                    ctrl.vm.addCatalog!==0
                }else{
                    toaster.info('提示',res.message);
                }
            })
        },
        addCataa:function(level){
            ctrl.vm.addCatalog =1;
            ctrl.vm.addData.level = level
        },
        saveAddChange:function(){
            $serCatalog.addIt({
                name:ctrl.vm.addData.name,
                status:ctrl.vm.addData.status,
                parent:ctrl.vm.addData.howList||0,
                sort:ctrl.vm.addData.sort,
                level:ctrl.vm.addData.level,
                is_hot:ctrl.vm.addData.is_hot,

                image:ctrl.vm.addData.image,
                icon:ctrl.vm.addData.icon
            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.addData = {};
                    toaster.success('提示',res.message);
                    vm.init();
                }else{
                    toaster.info('提示',res.message);
                }
            })
        },
        addTwo:function(id,level){
            ctrl.vm.addCatalog = 1;
            ctrl.vm.addData.howList = id;
            ctrl.vm.addData.level = Number(level)+1;
        },
        bianJiFun:function(level){
            ctrl.vm.bianJi =1;
            ctrl.vm.addData.level = Number(level);
        },
        delThis:function(id){  //删除分类,只能从下级往上级删
            var modalInstance = $uibModal.open({
                template: '<div class="modal-header"><h3 class="modal-title">确定删除此分类？</h3></div>' +
                '<div class="modal-footer"><button class="btn btn-sm btn-danger" ng-click="cancel()">Cancel</button>' +
                '<button class="btn btn-sm btn-success" ng-click="ok()">OK</button></div>',
                controller: 'catalogDelCtrl',
                windowClass: 'catalogDel',
                size: 'sm'
            });
            modalInstance.result.then(function (res) {
                var type='';
                if(res){
                    $serCatalog.delIt({
                        id : id
                    }).then(function(res){
                        switch (res.code){
                            case 1:type = 'success';break;
                            default:type = 'error';break;
                        }
                        toaster.pop(type, '信息', res.message);
                        vm.init();
                        ctrl.vm.daP.id=0 //清空信息栏
                        ctrl.vm.bianJi!==0
                        ctrl.vm.addCatalog!==0
                    })
                }
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        }
    };
    //添加/编辑分类图片,icon上传
    var uploader = $scope.uploader = $serImgUpload.setImgUpload('cat_', true);
    var uploaders = $scope.uploaders = $serImgUpload.setImgUpload('cat_icon_', true);
    $scope.clearItems = function (index) {
        if(index==1){
            $serImgUpload.putImgUpload(uploader, 'cat', function () {
                $serImgUpload.upload(uploader);
            });
            uploader.onSuccessItem = function (fileItem) {
                ctrl.vm.daP.image = fileItem.file.name;
                console.log(ctrl.vm.daP.image, 'ctrl.vm.daP.image');
            };
        }else if(index==2){
            $serImgUpload.putImgUpload(uploaders, 'cat_icon', function () {
                $serImgUpload.upload(uploaders);

            });
            uploaders.onSuccessItem = function (fileItem) {
                ctrl.vm.daP.icon = fileItem.file.name;
            };
        }else if(index==3){
            $serImgUpload.putImgUpload(uploader, 'cat', function () {
                $serImgUpload.upload(uploader);
            });
            uploader.onSuccessItem = function (fileItem) {
                ctrl.vm.addData.image = fileItem.file.name;
                console.log(ctrl.vm.addData.image, 'ctrl.vm.daP.image');
            };
        }else{
            $serImgUpload.putImgUpload(uploaders, 'cat_icon', function () {
                $serImgUpload.upload(uploaders);

            });
            uploaders.onSuccessItem = function (fileItem) {
                ctrl.vm.addData.icon = fileItem.file.name;
            };
        }
    }

    var vm ={
        init:function(){
            $serCatalog.getAll({

            }).then(function(res){
                if(res.code==1){
                    ctrl.vm.data = res.data;
                }else{
                    toaster.info('提示',res.message);
                }
            })
        }
    };
    vm.init();
});
myApp.controller('catalogDelCtrl', ['$scope', '$uibModalInstance', function($scope,$uibModalInstance) {
    $scope.ok = function () {
        $uibModalInstance.close(true);
    };
    $scope.cancel = function () {
        $uibModalInstance.dismiss(0);
    };
}]);